package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MachineAbnormalConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.Manned;
import com.tbit.main.service.MachineAbnormalService;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.MannedService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 载人设备绑定
 *
 * @author LMY
 * @create 2023-05-25 16:31
 */
@RestController
@RequestMapping("/manned")
public class MannedController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MannedService mannedService;
    @Autowired
    private MachineAbnormalService machineAbnormalService;

    /**
     * 添加
     */
    @RequestMapping("/addOrUpdate")
    @OperateLog(module = ModuleEnum.MAINMANNED, methods = MethodEnum.MANNED_ADD)
    public JsonResult<Object> addOrUpdate(String token, String userCode, String mannedNO, String serNo) {

        Assert.notNull(userCode, "Machine.userCode.notNull");
        Assert.notNull(mannedNO, "Machine.userCode.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUser.getAgents(), machine.getMachineId());

        mannedService.createOrUpdate(machine, mannedNO, serNo);
        return JsonResult.succ();
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.MAINMANNED, methods = MethodEnum.MANNED_DEL)
    public JsonResult<Object> del(String token, String userCode, String serNo) {

        Assert.notNull(userCode, "Machine.userCode.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        /**设备权限判断*/
        machineService.auth(accountUser.getAgents(), machine.getMachineId());
        mannedService.createOrUpdate(machine, null, serNo);
        machineAbnormalService.del(machine.getMachineId(), MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED);
        machineAbnormalService.del(machine.getMachineId(), MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED_LOSE);
        return JsonResult.succ();
    }


    /**
     * 根据区域查询
     */
    @RequestMapping("/getAccountId")
    public JsonResult<List<Manned>> getAccountId(Integer accountId) {
        return JsonResult.succ(mannedService.getAccountId(accountId));
    }

    /**
     * 根据车辆编号，载人编号查询
     */
    @RequestMapping("/getOne")
    public JsonResult<Manned> getOne(String userCode, String mannedNO) {
        /**查询设备信息*/
        mannedService.checkExistThrow(mannedNO);
        Machine machine = machineService.getByUseCode(userCode);
        if (machine == null) {
            return JsonResult.succ();
        }
        return JsonResult.succ(mannedService.getOne(machine.getMachineId()));
    }

}
